/**
 * Copyright 2013 Athenaeum, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.athenaeum.datastore.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.springframework.format.annotation.DateTimeFormat;

@Entity
@SuppressWarnings("serial")
public class User implements Serializable, Comparable<User> {

	@Id
	private String id;
	private String firstName;
	private String lastName;
	private String password;

	@DateTimeFormat(pattern = "dd/MM/yyyy")
	private Date dob;

	private boolean enabled;
	@MapKey
	@Cascade(CascadeType.ALL)
	@ManyToMany(mappedBy = "users", fetch = FetchType.EAGER)
	private Map<String, Project> projects = new HashMap<String, Project>();

	@MapKey
	@Cascade(CascadeType.ALL)
	@OneToMany(orphanRemoval = true, fetch = FetchType.EAGER, targetEntity = Experience.class)
	private Map<String, Experience> experiences = new HashMap<String, Experience>();

	@Transient
	private Experience experience;

	public String getFullName() {
		StringBuilder sb = new StringBuilder();
		sb.append(getFirstName()).append(" ").append(getLastName());
		return sb.toString();
	}

	@Override
	public int compareTo(User o) {
		return this.getFullName().compareTo(o.getFullName());
	}

	@Override
	public String toString() {
		return "User [firstName=" + firstName + ", lastName=" + lastName + "]";
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getDob() {
		return dob;
	}

	public void setDob(Date dob) {
		this.dob = dob;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public Map<String, Project> getProjects() {
		return projects;
	}

	public void setProjects(Map<String, Project> projects) {
		this.projects = projects;
	}

	public void addProject(Project project) {
		this.projects.put(project.getId(), project);
	}

	public Map<String, Experience> getExperiences() {
		return experiences;
	}

	public void addExperience(Experience experience) {
		this.experiences.put(experience.getId(), experience);
	}

	public void setExperiences(Map<String, Experience> experiences) {
		this.experiences = experiences;
	}

	public Experience getExperience() {
		return experience;
	}

	public void setExperience(Experience experience) {
		this.experience = experience;
	}

}
